package marcheba.itaka;

import java.io.IOException;
import java.net.URL;
import java.util.List;

import marcheba.itaka.entities.Itaka;
import marcheba.itaka.entities.ItakaServiceConfig;
import marcheba.itaka.entities.PhoneStatus;
import marcheba.itaka.entities.SpreadsheetConfig;
import marcheba.itaka.exeptions.ItakaException;

import com.apphance.android.Log;

import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.spreadsheet.ListFeed;
import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
import com.google.gdata.data.spreadsheet.SpreadsheetFeed;
import com.google.gdata.data.spreadsheet.WorksheetEntry;
import com.google.gdata.util.ServiceException;

public class SpreadsheetManager {

	private static final String TAG = "SpreadsheetManager";
	private SpreadsheetConfig spreadsheetConfig;
	private SpreadsheetService spreadsheetService;
	private SpreadsheetEntry spreadSheetEntry;
	private WorksheetEntry worksheetDataBaseEntry;
	private WorksheetEntry worksheetConfigEntry;

	public SpreadsheetManager(SpreadsheetConfig spreadsheetConfig) {
		this.spreadsheetConfig = spreadsheetConfig;
	}

	synchronized void connect() throws IOException, ServiceException,
			ItakaException {
		Log.i(TAG, "Connecting to spreadsheet service with param:"
				+ Itaka.APP_NAME);
		spreadsheetService = new SpreadsheetService(Itaka.APP_NAME);
		Log.i(TAG, "Login to spreadsheet service with login: "
				+ spreadsheetConfig.getUserCredentials().getAccountName());
		spreadsheetService.setUserCredentials(spreadsheetConfig
				.getUserCredentials().getAccountName(), spreadsheetConfig
				.getUserCredentials().getPassword());

		URL metafeedUrl = new URL(spreadsheetConfig.getSpreadSheetURL());

		SpreadsheetFeed feed = spreadsheetService.getFeed(metafeedUrl,
				SpreadsheetFeed.class);
		List<SpreadsheetEntry> spreadsheets = feed.getEntries();

		String str;
		for (int i = 0; i < spreadsheets.size(); i++) {
			SpreadsheetEntry entry = spreadsheets.get(i);

			str = entry.getTitle().getPlainText();
			if (str.equalsIgnoreCase(spreadsheetConfig.getSpreadsheetName()))
				spreadSheetEntry = entry;
		}

		if (spreadSheetEntry == null) {
			Log.e(TAG,
					"Can't find spreadsheet named: "
							+ spreadsheetConfig.getSpreadsheetName());
			throw new ItakaException("Can't find spreadsheet named: "
					+ spreadsheetConfig.getSpreadsheetName());
		}
		List<WorksheetEntry> worksheets = spreadSheetEntry.getWorksheets();

		worksheetDataBaseEntry = worksheets.get(spreadsheetConfig
				.getDataWorksheetNumber());
		worksheetConfigEntry = worksheets.get(spreadsheetConfig
				.getConfigWorksheetNumber());

		if (worksheetDataBaseEntry == null) {
			Log.e(TAG,
					"Can,t find worksheet numer"
							+ spreadsheetConfig.getDataWorksheetNumber());
			throw new ItakaException("Can,t find worksheet numer"
					+ spreadsheetConfig.getDataWorksheetNumber());
		}

		if (worksheetConfigEntry == null) {
			Log.e(TAG,
					"Can,t find worksheet numer"
							+ spreadsheetConfig.getConfigWorksheetNumber());
			throw new ItakaException("Can,t find worksheet numer"
					+ spreadsheetConfig.getConfigWorksheetNumber());
		}

	}

	synchronized ItakaServiceConfig loadServiceConfig() throws IOException,
			ServiceException {
		URL listFeedUrl = worksheetConfigEntry.getListFeedUrl();
		ListFeed feed = spreadsheetService.getFeed(listFeedUrl, ListFeed.class);
		return new ItakaServiceConfig(feed.getEntries());
	}

	synchronized void updateStatus(PhoneStatus phoneStatus) throws IOException,
			ServiceException, ItakaException {
		StatusUpdater statusUpdater = new StatusUpdater(spreadsheetService,
				worksheetDataBaseEntry.getListFeedUrl(),
				spreadsheetConfig.getResources());
		statusUpdater.updateStatus(phoneStatus);

	}

}
